let queue = ["apple", "pear"]
let queue1 = ["apple", "pear"]
let stack = ["apple", "pear"]
let stack1 = ["apple", "pear"]


// 队列练习
inbtn[0].addEventListener("click", function () {
    if (queueipt[0].value.trim()) {
        queue.unshift(queueipt[0].value)
        queuecont[0].innerHTML = "入队后队列内容：" + queue.join("->")
    }
})
outbtn[0].addEventListener("click", function () {
    if (queue.length) {
        queue.pop()
        queuecont[0].innerHTML = "出队后队列内容：" + queue.join("->")
    }
})
fontbtn[0].addEventListener("click", function () {
    if (queue.length) {
        let result = queue[queue.length - 1]
        queuecont[0].innerHTML = "队列内容：" + queue.join("->") + "<br>队头元素内容：" + result
    }
})
emptybtn[0].addEventListener("click", function () {
    let result = queue.length == 0 ? "队列内容为空" : "队列内容不为空"
    queuecont[0].innerHTML = "队列内容：" + queue.join("->") + "<br>" + result
})
inbtn[1].addEventListener("click", function () {
    if (queueipt[1].value.trim()) {
        queue.push(queueipt[1].value)
        queuecont[1].innerHTML = "入队后队列内容：" + queue.join("<-")
    }
})
outbtn[1].addEventListener("click", function () {
    if (queue.length) {
        queue.pop()
        queuecont[1].innerHTML = "出队后队列内容：" + queue.join("<-")
    }
})
fontbtn[1].addEventListener("click", function () {
    if (queue.length) {
        let result = queue[0]
        queuecont[1].innerHTML = "队列内容：" + queue.join("<-") + "<br>队头元素内容：" + result
    }
})
emptybtn[1].addEventListener("click", function () {
    let result = queue.length == 1 ? "队列内容为空" : "队列内容不为空"
    queuecont[1].innerHTML = "队列内容：" + queue.join("<-") + "<br>" + result
})

// 栈练习
pushbtn[0].addEventListener("click", function () {
    if (stackipt[0].value.trim()) {
        stack.unshift(stackipt[0].value)
        stackcont[0].innerHTML = "进栈后栈内容：" + stack.join("->")
    }
})
popbtn[0].addEventListener("click", function () {
    if (stack.length !== 0) {
        stack.pop()
        stackcont[0].innerHTML = "出栈后栈内容：" + stack.join("->")
    }
})
stackfontbtn[0].addEventListener("click", function () {
    if (stack.length !== 0) {
        let result = stack[stack.length - 1]
        stackcont[0].innerHTML = "栈内容：" + stack.join("->") + "<br>栈顶元素内容：" + result
    }
})
stackemptybtn[0].addEventListener("click", function () {
    let result = stack.length == 0 ? "栈内容为空" : "栈内容不为空"
    stackcont[0].innerHTML = "栈内容：" + stack.join("->") + "<br>" + result
})
pushbtn[1].addEventListener("click", function () {
    if (stackipt[1].value.trim()) {
        stack1.push(stackipt[1].value)
        stackcont[1].innerHTML = "进栈后栈内容：" + stack1.join("<-")
    }
})
popbtn[1].addEventListener("click", function () {
    if (stack1.length !== 0) {
        stack1.shift()
        stackcont[1].innerHTML = "出栈后栈内容：" + stack1.join("<-")
    }
})
stackfontbtn[1].addEventListener("click", function () {
    if (stack1.length !== 0) {
        let result = stack1[0]
        stackcont[1].innerHTML = "栈内容：" + stack1.join("<-") + "<br>栈顶元素内容：" + result
    }
})
stackemptybtn[1].addEventListener("click", function () {
    let result = stack1.length == 0 ? "栈内容为空" : "栈内容不为空"
    stackcont[1].innerHTML = "栈内容：" + stack1.join("<-") + "<br>" + result
})

let sortresult = document.querySelector("#sortresult")
let arr1 = [43, 54, 4, -4, 84, 100, 58, 27, 140] // 从大到小以及从小到大进行排序
let arr2 = ['apple', 'dog', 'cat', 'car', 'zoo', 'orange', 'airplane'] // 按字母顺序a-z及z-a进行排序
let arr3 = [[10, 14], [16, 60], [7, 44], [26, 35], [22, 63]] // 按照每个元素中第二个数从大到小的顺序进行排序输出
let arr4 = [
    {
        id: 1,
        name: 'candy',
        value: 40
    }, {
        id: 2,
        name: 'Simon',
        value: 50
    }, {
        id: 3,
        name: 'Tony',
        value: 45
    }, {
        id: 4,
        name: 'Annie',
        value: 60
    }
]  // 按元素对象的value值从小到大进行排序

let arr1max = arr1
let arr1min = arr1
for (let i = 0; i < arr1max.length; i++) {
    for (let j = 0; j < arr1max.length - i; j++) {
        if (arr1max[j] < arr1max[j + 1]) {
            let temp = arr1max[j]
            arr1max[j] = arr1max[j + 1]
            arr1max[j + 1] = temp
        }
    }
}
sortresult.textContent = arr1max + "\n\n"
console.log("从大到小进行排序")
console.log(arr1max);
arr1min.sort(function (a, b) {
    return a - b
})
sortresult.textContent += arr1min + "\n\n"
console.log("从小到大进行排序")
console.log(arr1min);

let azarr2 = arr2
let zaarr2 = arr2
azarr2.sort(function (a, b) {
    if (a < b) {
        return -1
    } else if (a = b) {
        return 0
    } else if (a > b) {
        return 1
    }
})
sortresult.textContent += azarr2 + "\n\n"
console.log("按字母顺序a-z进行排序")
console.log(azarr2);
zaarr2 = azarr2.reverse()
sortresult.textContent += zaarr2 + "\n\n"
console.log("按字母顺序z-a进行排序")
console.log(zaarr2);

for (let i = 0; i < arr3.length; i++) {
    for (let j = 0; j < arr3.length - i - 1; j++) {
        if (arr3[j][1] < arr3[j + 1][1]) {
            let temp = arr3[j]
            arr3[j] = arr3[j + 1]
            arr3[j + 1] = temp
        }
    }
}
sortresult.textContent += arr3 + "\n\n"
console.log("按照每个元素中第二个数从大到小的顺序进行排序输出：")
console.log(arr3);

for (let i = 0; i < arr4.length; i++) {
    for (let j = 0; j < arr4.length - i - 1; j++) {
        if (arr4[j].value > arr4[j + 1].value) {
            let temp = arr4[j]
            arr4[j] = arr4[j + 1]
            arr4[j + 1] = temp
        }
    }
}
sortresult.textContent += arr4
console.log("按数组对象中的value进行从小到大的排序：")
console.log(arr4);


let scoreObject = {
    "Tony": {
        "Math": 95,
        "English": 79,
        "Music": 68
    },
    "Simon": {
        "Math": 100,
        "English": 95,
        "Music": 98
    },
    "Annie": {
        "Math": 54,
        "English": 65,
        "Music": 88
    }
}
let scoreArray = []
for (const k in scoreObject) {
    let arr = []
    const v = scoreObject[k];
    arr.push(k)
    for (const kk in v) {
        const element = v[kk];
        arr.push(element)
    }
    scoreArray.push(arr)
}
console.log("将对象转为二维数组：")
console.log(scoreArray);

let menuArr = [
    [1, "Area1", -1],
    [2, "Area2", -1],
    [3, "Area1-1", 1],
    [4, "Area1-2", 1],
    [5, "Area2-1", 2],
    [6, "Area2-2", 2],
    [7, "Area1-2-3", 4],
    [8, "Area2-2-1", 6],
]
function getMenuObj(arr, root = -1) {
    var obj = {};
    for (let i = 0; i < arr.length; i++) {
        let subArr = arr[i]
        if (subArr[2] == root) {
            obj[subArr[0]] = {}
            obj[subArr[0]].name = subArr[1]
            let tempObj = getMenuObj(arr, subArr[0])
            if (JSON.stringify(tempObj) != "{}") {
                obj[subArr[0]].subMenu = tempObj
            }
        }
    }
    return obj;
}
let arr = getMenuObj(menuArr)
console.log("多级菜单数据的数组转换：")
console.log(arr)


// function getarr(arr) {
//     for (const k in arr) {
//         console.log("\t" + k);
//         if (arr[k] instanceof Object) {
//             getarr(arr[k])
//         } else {
//             console.log("\t\t" + arr[k]);
//         }
//     }
// }
// getarr(arr)
